﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySkiPatrolEntity;
using MySkiPatrolDAL;
using MySkiPatrol.Security;

namespace MySkiPatrolBLL
{
    public class MembershipBLO
    {
        MembershipDAO dao = new MembershipDAO();
        PatrolDAO daoPatrol = new PatrolDAO();

        //private string APP_KEY;
        //private string APP_SALT;
        //private string APP_EMAIL_ADDRESS;
        //private string APP_EMAIL_HOST;
        //private string APP_EMAIL_PASSWORD;
        //private string APP_TITLE;

        public MembershipBLO()
        {
            //List<AppSetting> settings = daoPatrol.GetAppSettings(data);

            //foreach (AppSetting setting in settings)
            //{
            //    switch (setting.Name.ToUpper())
            //    {
            //        case "DEBUG_EMAIL_ENABLED":
            //            //DEBUG_ENABLED = DataConvertor.toBool(setting.Value);
            //            break;
            //        case "DEBUG_EMAIL_ADDR":
            //            //DEBUG_EMAIL_ADDR = setting.Value;
            //            break;
            //        case "APPCYPHER":
            //            APP_KEY = setting.Value;
            //            break;
            //        case "APPKEY":
            //            APP_SALT = setting.Value;
            //            break;
            //        case "APPEMAILADDRESS":
            //            APP_EMAIL_ADDRESS = setting.Value;
            //            break;
            //        case "APPEMAILHOST":
            //            APP_EMAIL_HOST = setting.Value;
            //            break;
            //        case "APPEMAILPASSWORD":
            //            APP_EMAIL_PASSWORD = setting.Value;
            //            break;
            //        case "APPTITLE":
            //            APP_TITLE = setting.Value;
            //            break;

            //        default:
            //            break;
            //    }
            //}
        }

        public WebAccess Login(WebAccess data)
        {
            string pwdCheck = data.Password.ToString();

            data = dao.Login(data);

            if (data.DataTransaction.Success == true)
            {
                //TODO: decryption



                //dao was good...is the password?
                if (pwdCheck == Core.Decrypt(data.Password))
                {

                }
                else
                {
                    data.DataTransaction.Success = false;
                    if (data.DataTransaction.TransactionErrors == null) data.DataTransaction.TransactionErrors = new List<Error>();
                    data.DataTransaction.TransactionErrors.Add(new Error("BLO11000", "The password provide is not correct for the username."));
                }
            }

            //clear the password as this should not be returned...
            data.Password = null;
            return data;
        }

        public Member Logout(Member data)
        {
            return dao.Logout(data);
        }

        public Patrol GetMembers(Patrol data)
        {
            //validation logic???
            if (data == null)
            {
                data = new Patrol();
                data.DataTransaction = new DataTransaction();
                data.DataTransaction.Success = false;
                data.DataTransaction.TransactionErrors = new List<Error>();
                data.DataTransaction.TransactionErrors.Add(new Error("", "You must provide a Patrol ID"));
            }

            if (data.DataTransaction == null) data.DataTransaction = new DataTransaction();
            if (data.DataTransaction.TransactionErrors == null) data.DataTransaction.TransactionErrors = new List<Error>();

            if (string.IsNullOrEmpty(data.ID))
            {
                data.DataTransaction.Success = false;
                data.DataTransaction.TransactionErrors.Add(new Error("", "You must provide a Patrol ID"));
            }
            else
            {
                // make sure we have the database information...
                if(string.IsNullOrEmpty(data.DataTransaction.PatrolPrefix))
                {
                    //get the patrol prefix from the patrol table...
                    Patrol data1 = daoPatrol.GetPatrolByID(data);
                    data.DataTransaction.PatrolPrefix = data1.dbPrefix;
                }

                try
                {
                    data = dao.GetMembers(data);
                    data.DataTransaction.Success = true;
                }
                catch (Exception ex)
                {
                    data.DataTransaction.Success = false;
                    if (data.DataTransaction.TransactionErrors == null) data.DataTransaction.TransactionErrors = new List<Error>();
                    data.DataTransaction.TransactionErrors.Add(new Error("00000", ex.Message));
                }
            }

            if (data.DataTransaction.Success)
            {

                //foreach (Member member in data.Members)
                //{
                    
                //    //attach the member's roles
                //    try
                //    {

                //        member.Roles = dao.GetMemberRoles(member);
                //    }
                //    catch (Exception ex)
                //    {

                //    }

                //    //attach the member's groups
                //    try
                //    {

                //        member.Groups = dao.GetMemberGroups(member);
                //    }
                //    catch (Exception ex)
                //    {

                //    }
                //}
            }

            return data;
        }

        public Member GetMember(Member data)
        {
            //validation logic???
            if (data == null)
            {
                data = new Member();
                data.DataTransaction = new DataTransaction();
                data.DataTransaction.Success=false;
                data.DataTransaction.TransactionErrors = new List<Error>();
                data.DataTransaction.TransactionErrors.Add(new Error("", "You must provide a member ID"));
            }

            if (data.DataTransaction == null) data.DataTransaction = new DataTransaction();
            if (data.DataTransaction.TransactionErrors == null) data.DataTransaction.TransactionErrors = new List<Error>();

            if (string.IsNullOrEmpty(data.ID))
            {
                data.DataTransaction.Success = false;
                data.DataTransaction.TransactionErrors.Add(new Error("", "You must provide a member ID"));
            }
            else
            {
                data = dao.GetMember(data);
            }

            if (data.DataTransaction.Success)
            {
                //attach the member's roles
                data.Roles = dao.GetMemberRoles(data);

                //attach the member's groups
                data.Groups = dao.GetMemberGroups(data);

                //attach the member's groups
                data.IDCards = dao.GetMemberIDCards(data);

            }

            return data;
        }

        public Member PutMember(Member data)
        {
            //validation logic???
            if (data == null)
            {
                data = new Member();
                data.DataTransaction = new DataTransaction();
                data.DataTransaction.Success = false;
                data.DataTransaction.TransactionErrors = new List<Error>();
                data.DataTransaction.TransactionErrors.Add(new Error("", "You must provide a Patrol ID"));
            }

            if (data.DataTransaction == null) data.DataTransaction = new DataTransaction();
            if (data.DataTransaction.TransactionErrors == null) data.DataTransaction.TransactionErrors = new List<Error>();

            //if (string.IsNullOrEmpty(data.ID))
            //{
            //    data.DataTransaction.Success = false;
            //    data.DataTransaction.TransactionErrors.Add(new Error("", "You must provide a Patrol ID"));
            //}
            //else
            {
                switch (data.DataTransaction.Action)
                {
                    case DataAction.Add:
                        data.ID = Guid.NewGuid().ToString();
                        data = dao.AddMember(data);

                        if (data.DataTransaction.Success)
                        {
                            //add the ID Cards
                            if ((data.IDCards != null) && (data.IDCards.Count > 0))
                            {
                                foreach (IDCard card in data.IDCards)
                                {
                                    card.DataTransaction = data.DataTransaction;
                                    card.MemberID = data.ID;

                                    IDCard addCard = PutIDCard(card);
                                    card.DataTransaction.Success = addCard.DataTransaction.Success;
                                    addCard = null;
                                }
                            }
                        }

                        break;
                    case DataAction.Delete:
                        dao.DeleteMember(data);
                        break;
                    case DataAction.Update:
                        dao.UpdateMember(data);
                        break;
                }
            

            }
            return data;
        }

        public IDCard PutIDCard(IDCard data)
        {
            //validation logic???
            if (data == null)
            {
                data = new IDCard();
                data.DataTransaction = new DataTransaction();
                data.DataTransaction.Success = false;
                data.DataTransaction.TransactionErrors = new List<Error>();
                data.DataTransaction.TransactionErrors.Add(new Error("", "You must provide a Patrol ID"));
            }

            if (data.DataTransaction == null) data.DataTransaction = new DataTransaction();
            if (data.DataTransaction.TransactionErrors == null) data.DataTransaction.TransactionErrors = new List<Error>();

            //if (string.IsNullOrEmpty(data.ID))
            //{
            //    data.DataTransaction.Success = false;
            //    data.DataTransaction.TransactionErrors.Add(new Error("", "You must provide a Patrol ID"));
            //}
            //else
            {
                switch (data.DataTransaction.Action)
                {
                    case DataAction.Add:
                        data = dao.AddMemberIDCard(data);
                        break;
                    case DataAction.Delete:
                        data = dao.DeleteMemberIDCard(data);
                        if (data.DataTransaction.Success) data = dao.GetMemberIDCard(data);
                        break;
                    case DataAction.Update:
                        //dao.UpdateMember(data);
                        break;
                }
                //what is required for a new member?



            }
            return data;
        }
    }
}
